<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>图片懒加载手写(可视区域加载)</title>
    <style>
            * {
            padding: 0px;
            margin: 0px;
            }
             
            html,
            body {
            width: 100%;
            min-height: 100%;
            }
             
            #ab {
            margin: 0;
            padding: 0;
            list-style: none;
            }
             
            #ab .in {
            border: 1px solid red;
            margin: 10px;
            text-align: center;
            height: 400px;
            width: 400px;
            /* float: left; */
            }
             
            .in img {
            border: none;
            vertical-align: middle;
            height: 400px;
            width: 400px;
            }
           </style>
</head>
<body>
    <ul id="ab">
        <li class="in"><img src="" alt="" data-imgurl="0.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="1.jpeg"></li>
        <li class="in"><img src="" alt="" data-imgurl="2.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="3.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="0.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="1.jpeg"></li>
        <li class="in"><img src="" alt="" data-imgurl="2.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="3.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="0.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="1.jpeg"></li>
        <li class="in"><img src="" alt="" data-imgurl="2.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="3.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="0.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="1.jpeg"></li>
        <li class="in"><img src="" alt="" data-imgurl="2.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="0.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="1.jpeg"></li>
        <li class="in"><img src="" alt="" data-imgurl="2.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="3.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="0.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="1.jpeg"></li>
        <li class="in"><img src="" alt="" data-imgurl="2.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="3.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="0.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="1.jpeg"></li>
        <li class="in"><img src="" alt="" data-imgurl="2.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="3.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="0.jpg"></li>
        <li class="in"><img src="" alt="" data-imgurl="1.jpeg"></li>
        <li class="in"><img src="" alt="" data-imgurl="2.jpg"></li>
    </ul>

    <script type="text/javascript">
        var aImages = document.getElementById('ab').getElementsByTagName('img');//获取到图片数组
        loadImg(aImages);//
        window.onscroll = function() {//滚动条滚动触发事件
            loadImg(aImages);
        };

        //图片懒加载的核心   getBoundingClientRect()  返回一个对象，相对于可视窗口左上角的上右下左数据
        function loadImg(arr) {//遍历图片数组
            for(var i = 0;i<arr.length;i++) {
                //document.documentElement.clientHeight 获取文档元素在浏览器的高度  如果图片顶部距离浏览器窗口的距离<文档在浏览器的高度【图片在可视区域范围内】；并且图片已加载，就显示图片   
                if(arr[i].getBoundingClientRect().top < document.documentElement.clientHeight && !arr[i].isLoad) {
                    arr[i].isLoad = true;//图片显示标志位
                    (function(i) {    //(function(i){},500)(i);此处的i表示理解调用function(i){}函数
                        // setTimeout(function(){},500)在500ms后调用function函数
                        setTimeout(function() {//兼容不支持data的浏览器
                            if(arr[i].dataset) {//如果浏览器支持data
                                aftLoadImg(arr[i],arr[i].dataset.imgurl);
                            } else {//如果浏览器不支持data 用.getAttribute("data-imgurl")表示图片路径
                                aftLoadImg(arr[i],arr[i].getAttribute("data-imgurl"));
                            }
                            arr[i].style.cssText = "transition: 1s;opacity:1;"//相当于fadein 1s后图片完全显示
                        },500)
                    })(i);
                }
            }
        }

        function aftLoadImg(obj,url) {
            var oImg = new Image();//建立图像对象
            oImg.src = url;//将图片路径给对象
            oImg.onload = function() {
                obj.src = oImg.src;
            } 
        }
    </script>
</body>
</html>